/*
 * Created by 2006-7-10 下午11:30:03
 * $Id$
 */
package com.someok.service.photo;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.someok.service.photo.domain.Album163;
import com.someok.service.photo.domain.Photo163;
import com.someok.service.photo.service.Album163Manager;
import com.someok.service.photo.service.Photo163Manager;
import com.someok.service.photo.spider.PhotoSpiderFor163;

/**
 * 
 * <p>
 * <a href="Album163Facade.java.html"><i>View Source</i></a>
 * </p>
 * 
 * @author wjx
 * @version $Revision$ $Date$
 */
public class Album163Facade {
	String userId;
	
	/** 每页显示的图片数量. */
	int photoNumPerPage = 0;

	Album163Manager album163Manager;

	Photo163Manager photo163Manager;

	// static ConfigurableApplicationContext ctx = null;
	//
	// static {
	// ctx = new ClassPathXmlApplicationContext(new String[] {
	// "classpath:applicationContext.xml",
	// "classpath:applicationContext-hibernate.xml",
	// "classpath:applicationContext-manager.xml" });
	//
	// }

	public Album163Facade() {

	}

	public Album163Facade(String userId) {
		this.userId = userId;
		// album163Manager = (Album163Manager) ctx.getBean("album163Manager");
		// photo163Manager = (Photo163Manager) ctx.getBean("photo163Manager");
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public int getPhotoNumPerPage() {
		return photoNumPerPage;
	}

	public void setPhotoNumPerPage(int photoNumPerPage) {
		this.photoNumPerPage = photoNumPerPage;
	}

	public Album163Manager getAlbum163Manager() {
		return album163Manager;
	}

	public void setAlbum163Manager(Album163Manager album163Manager) {
		this.album163Manager = album163Manager;
	}

	public Photo163Manager getPhoto163Manager() {
		return photo163Manager;
	}

	public void setPhoto163Manager(Photo163Manager photo163Manager) {
		this.photo163Manager = photo163Manager;
	}

	public static void main(String[] args) {
		Album163Facade facade = new Album163Facade("someok4u");
		facade.exec();
	}

	/**
	 * TODO:需要在插入之前对重复记录做删除操作
	 * 
	 */
	public void exec() {
		PhotoSpiderFor163 spider = new PhotoSpiderFor163(this.userId);
		List albumList = spider.forageAlbums();
		List photoList = null;

		album163Manager.saveOrUpdateAll(albumList);

		// 将相册插入db
		for (Iterator albumIter = albumList.iterator(); albumIter.hasNext();) {
			Album163 album = (Album163) albumIter.next();
			if (album == null || StringUtils.isBlank(album.getAlbum163Id())) {
				continue;
			}
			// album163Manager.saveOrUpdate(album);
			System.out.println(album);

			// 得到相册下面的所有照片
			photoList = spider.foragePhotos(album.getAlbum163Id());
			if (photoList != null && photoList.size() > 0) {
				photo163Manager.saveOrUpdateAll(photoList);

				for (Iterator photoIter = photoList.iterator(); photoIter
						.hasNext();) {
					Photo163 photo = (Photo163) photoIter.next();
					// photo163Manager.saveOrUpdate(photo);
					System.out.println(photo);
				}
			}

		}

	}

	/**
	 * 取得所有相册信息.
	 * @return
	 */
	public List getAllAlbum() {
		System.out.println("photoNumPerPage : " + photoNumPerPage);
		return album163Manager.getAll();
	}
	
	public List getPhotos() {
		return null;
	}
}
